home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / tsipp / tsipp.lha / tsipp3.0a / src / tSippPrim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-02  |  17.7 KB  |  511 lines

  1. /*
  2.  *=============================================================================
  3.  *                                  tSippPrim.c
  4.  *-----------------------------------------------------------------------------
  5.  * Tcl commands to create basic SIPP primitive objects. 
  6.  *-----------------------------------------------------------------------------
  7.  * Copyright 1992 Mark Diekhans
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies.  Mark Diekhans makes
  11.  * no representations about the suitability of this software for any purpose.
  12.  * It is provided "as is" without express or implied warranty.
  13.  *-----------------------------------------------------------------------------
  14.  * $Id: tSippPrim.c,v 2.0 1992/11/02 03:56:31 markd Rel $
  15.  *=============================================================================
  16.  */
  17.  
  18. #include "tSippInt.h"
  19.  
  20. /*=============================================================================
  21.  * SippTorus --
  22.  *   Implements the command:
  23.  *     SippTorus bigradius smallradius radialres tuberes shaderhandle [texture]
  24.  * Note:
  25.  *   This procedure has standard Tcl command calling sematics.  ClientData
  26.  * contains a pointer to the Tcl SIPP global structure.
  27.  *-----------------------------------------------------------------------------
  28.  */
  29. int
  30. SippTorus (clientData, interp, argc, argv)
  31.     char       *clientData;
  32.     Tcl_Interp *interp;
  33.     int         argc;
  34.     char      **argv;
  35. {
  36.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  37.     Shader        *shaderPtr;
  38.     void          *surfDescPtr;
  39.     double         bigRadius, smallRadius;
  40.     unsigned       radialRes, tubeRes;
  41.     int            texture = NATURAL;
  42.  
  43.     if ((argc < 6) || (argc > 7)) {
  44.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  45.                           " bigradius smallradius radialres tuberes ",
  46.                           "shaderhandle [texture]", (char *) NULL);
  47.         return TCL_ERROR;
  48.     }                     
  49.  
  50.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [1], &bigRadius))
  51.         return TCL_ERROR;
  52.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [2], &smallRadius))
  53.         return TCL_ERROR;
  54.     if (smallRadius >= bigRadius) {
  55.         Tcl_AppendResult (interp, "big radius must be greater than small ",
  56.                           "radius", (char *) NULL);
  57.         return TCL_ERROR;
  58.     }
  59.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [3], &radialRes))
  60.         return TCL_ERROR;
  61.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [4], &tubeRes))
  62.         return TCL_ERROR;
  63.  
  64.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [5],
  65.                                         &surfDescPtr);
  66.     if (shaderPtr == NULL)
  67.         return TCL_ERROR;
  68.  
  69.     if (argc == 7) {
  70.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [6], &texture, NULL))
  71.             return TCL_ERROR;
  72.     }
  73.  
  74.     TSippBindObjectToHandle (tSippGlobPtr, 
  75.                              sipp_torus (bigRadius, smallRadius, radialRes,
  76.                                          tubeRes, surfDescPtr, shaderPtr,
  77.                                          texture)); 
  78.                       
  79.     return TCL_OK;
  80.  
  81. } /* SippTorus */
  82.  
  83. /*=============================================================================
  84.  * SippCone --
  85.  *   Implements the command:
  86.  *     SippCone bottomradius topradius length resolution shaderhandle [texture]
  87.  * Note:
  88.  *   This procedure has standard Tcl command calling sematics.  ClientData
  89.  * contains a pointer to the Tcl SIPP global structure.
  90.  *-----------------------------------------------------------------------------
  91.  */
  92. int
  93. SippCone (clientData, interp, argc, argv)
  94.     char       *clientData;
  95.     Tcl_Interp *interp;
  96.     int         argc;
  97.     char      **argv;
  98. {
  99.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  100.     Shader        *shaderPtr;
  101.     void          *surfDescPtr;
  102.     double         bottomRadius, topRadius, length;
  103.     unsigned       resolution;
  104.     int            texture = NATURAL;
  105.  
  106.     if ((argc < 6) || (argc > 7)) {
  107.         Tcl_AppendResult (interp, "wrong # args: ", argv [0], " bottomgradius",
  108.                           " topradius length resolution shaderhandle ",
  109.                           "[texture]", (char *) NULL);
  110.         return TCL_ERROR;
  111.     }                     
  112.  
  113.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [1], &bottomRadius))
  114.         return TCL_ERROR;
  115.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [2], &topRadius))
  116.         return TCL_ERROR;
  117.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [3], &length))
  118.         return TCL_ERROR;
  119.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [4], &resolution))
  120.         return TCL_ERROR;
  121.  
  122.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [5],
  123.                                         &surfDescPtr);
  124.     if (shaderPtr == NULL)
  125.         return TCL_ERROR;
  126.  
  127.     if (argc == 7) {
  128.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [6], &texture, NULL))
  129.             return TCL_ERROR;
  130.     }
  131.  
  132.     TSippBindObjectToHandle (tSippGlobPtr, 
  133.                              sipp_cone (bottomRadius, topRadius, length,
  134.                                         resolution, surfDescPtr, shaderPtr,
  135.                                         texture)); 
  136.                       
  137.     return TCL_OK;
  138.  
  139. } /* SippCone */
  140.  
  141. /*=============================================================================
  142.  * SippCylinder --
  143.  *   Implements the command:
  144.  *     SippCylinder radius length resolution shaderhandle [texture]
  145.  * Note:
  146.  *   This procedure has standard Tcl command calling sematics.  ClientData
  147.  * contains a pointer to the Tcl SIPP global structure.
  148.  *-----------------------------------------------------------------------------
  149.  */
  150. int
  151. SippCylinder (clientData, interp, argc, argv)
  152.     char       *clientData;
  153.     Tcl_Interp *interp;
  154.     int         argc;
  155.     char      **argv;
  156. {
  157.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  158.     Shader        *shaderPtr;
  159.     void          *surfDescPtr;
  160.     double         radius, length;
  161.     int            resolution, texture = NATURAL;
  162.  
  163.     if ((argc < 5) || (argc > 6)) {
  164.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  165.                           " radius length resolution shaderhandle [texture]",
  166.                           (char *) NULL);
  167.         return TCL_ERROR;
  168.     }                     
  169.  
  170.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [1], &radius))
  171.          return TCL_ERROR;
  172.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [2], &length))
  173.         return TCL_ERROR;
  174.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [3], &resolution))
  175.         return TCL_ERROR;
  176.  
  177.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [4],
  178.                                         &surfDescPtr);
  179.     if (shaderPtr == NULL)
  180.         return TCL_ERROR;
  181.  
  182.     if (argc == 6) {
  183.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [5], &texture, NULL))
  184.             return TCL_ERROR;
  185.     }
  186.  
  187.     TSippBindObjectToHandle (tSippGlobPtr,
  188.                              sipp_cylinder (radius, length, resolution,
  189.                                             surfDescPtr, shaderPtr,
  190.                                             texture)); 
  191.     return TCL_OK;
  192.  
  193. } /* SippCylinder */
  194.  
  195. /*=============================================================================
  196.  * SippEllipsoid --
  197.  *   Implements the command:
  198.  *     SippEllipsoid {xradius yradius zradius} resolution shaderhandle
  199.  *                   [texture]
  200.  * Note:
  201.  *   This procedure has standard Tcl command calling sematics.  ClientData
  202.  * contains a pointer to the Tcl SIPP global structure.
  203.  *-----------------------------------------------------------------------------
  204.  */
  205. int
  206. SippEllipsoid (clientData, interp, argc, argv)
  207.     char       *clientData;
  208.     Tcl_Interp *interp;
  209.     int         argc;
  210.     char      **argv;
  211. {
  212.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  213.     Shader        *shaderPtr;
  214.     void          *surfDescPtr;
  215.     Vector         radius;
  216.     unsigned       resolution;
  217.     int            texture = NATURAL;
  218.  
  219.     if ((argc < 4) || (argc > 5)) {
  220.         Tcl_AppendResult (interp, "wrong # args: ", argv [0], 
  221.                           " {xradius yradius zradius} resolution ",
  222.                           "shaderhandle [texture]", (char *) NULL);
  223.         return TCL_ERROR;
  224.     }                     
  225.  
  226.     if (!TSippConvertVertex (tSippGlobPtr, argv [1], &radius))
  227.         return TCL_ERROR;
  228.     if ((radius.x <= 0.0) || (radius.y <= 0.0) || (radius.z <= 0.0)) {
  229.         Tcl_AppendResult (tSippGlobPtr->interp, " radius X Y and Z must be ",
  230.                           " > 0.0", (char *) NULL);
  231.         return TCL_ERROR;
  232.     }
  233.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [2], &resolution))
  234.         return TCL_ERROR;
  235.  
  236.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [3],
  237.                                         &surfDescPtr);
  238.     if (shaderPtr == NULL)
  239.         return TCL_ERROR;
  240.  
  241.     if (argc == 5) {
  242.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [4], &texture, NULL))
  243.             return TCL_ERROR;
  244.     }
  245.  
  246.     TSippBindObjectToHandle (tSippGlobPtr, 
  247.                              sipp_ellipsoid (radius.x, radius.y, radius.z,
  248.                                              resolution, surfDescPtr,
  249.                                              shaderPtr, texture)); 
  250.     return TCL_OK;
  251.  
  252. } /* SippEllipsoid */
  253.  
  254. /*=============================================================================
  255.  * SippSphere --
  256.  *   Implements the command:
  257.  *     SippSphere radius resolution shaderhandle [texture]
  258.  * Note:
  259.  *   This procedure has standard Tcl command calling sematics.  ClientData
  260.  * contains a pointer to the Tcl SIPP global structure.
  261.  *-----------------------------------------------------------------------------
  262.  */
  263. int
  264. SippSphere (clientData, interp, argc, argv)
  265.     char       *clientData;
  266.     Tcl_Interp *interp;
  267.     int         argc;
  268.     char      **argv;
  269. {
  270.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  271.     Shader        *shaderPtr;
  272.     void          *surfDescPtr;
  273.     double         radius;
  274.     int            resolution, texture = NATURAL;
  275.  
  276.     if ((argc < 4) || (argc > 5)) {
  277.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  278.                           " radius resolution shaderhandle [texture]",
  279.                           (char *) NULL);
  280.         return TCL_ERROR;
  281.     }                     
  282.  
  283.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [1], &radius))
  284.          return TCL_ERROR;
  285.     if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [2], &resolution))
  286.         return TCL_ERROR;
  287.  
  288.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [3],
  289.                                         &surfDescPtr);
  290.     if (shaderPtr == NULL)
  291.         return TCL_ERROR;
  292.  
  293.     if (argc == 5) {
  294.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [4], &texture, NULL))
  295.             return TCL_ERROR;
  296.     }
  297.  
  298.     TSippBindObjectToHandle (tSippGlobPtr,
  299.                              sipp_sphere (radius, resolution,
  300.                                           surfDescPtr, shaderPtr, texture)); 
  301.     return TCL_OK;
  302.  
  303. } /* SippSphere */
  304.  
  305. /*=============================================================================
  306.  * SippPrism --
  307.  *   Implements the command:
  308.  *     SippPrism 2dpointlist length shaderhandle [texture]
  309.  * Note:
  310.  *   This procedure has standard Tcl command calling sematics.  ClientData
  311.  * contains a pointer to the Tcl SIPP global structure.
  312.  *-----------------------------------------------------------------------------
  313.  */
  314. int
  315. SippPrism (clientData, interp, argc, argv)
  316.     char       *clientData;
  317.     Tcl_Interp *interp;
  318.     int         argc;
  319.     char      **argv;
  320. {
  321.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  322.     Shader        *shaderPtr;
  323.     void          *surfDescPtr;
  324.     double         length;
  325.     int            listArgc, idx;
  326.     char         **listArgv;
  327.     Vector        *pointList;
  328.     int            texture = NATURAL;
  329.  
  330.     if ((argc < 4) || (argc > 5)) {
  331.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  332.                           " 2dpointlist length shaderhandle [texture]",
  333.                           (char *) NULL);
  334.         return TCL_ERROR;
  335.     }                     
  336.  
  337.     if (Tcl_SplitList (tSippGlobPtr->interp, argv [1], &listArgc,
  338.                        &listArgv) != TCL_OK)
  339.         return TCL_ERROR;
  340.  
  341.     if (listArgc < 3) {
  342.         ckfree (listArgv);
  343.         Tcl_AppendResult (interp, "2d point list must contain at least 3 ",
  344.                           "points", (char *) NULL);
  345.         return TCL_ERROR;
  346.     }
  347.  
  348.     pointList = (Vector *) ckalloc (listArgc * sizeof (Vector));
  349.  
  350.     for (idx = 0; idx < listArgc; idx++) {
  351.         if (!TSippConvert2DPoint (tSippGlobPtr, listArgv [idx], 
  352.                                   &pointList [idx].x, &pointList [idx].y))
  353.             goto errorExit;
  354.         pointList [idx].z = 0.0;
  355.     }
  356.  
  357.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [2], &length))
  358.         goto errorExit;
  359.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [3],
  360.                                         &surfDescPtr);
  361.     if (shaderPtr == NULL)
  362.         goto errorExit;
  363.  
  364.     if (argc == 5) {
  365.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [4], &texture, NULL))
  366.             goto errorExit;
  367.     }
  368.  
  369.     TSippBindObjectToHandle (tSippGlobPtr,
  370.                              sipp_prism (listArgc, pointList, length,
  371.                                          surfDescPtr, shaderPtr, texture)); 
  372.     ckfree (pointList);
  373.     ckfree (listArgv);
  374.     return TCL_OK;
  375. errorExit:
  376.     ckfree (pointList);
  377.     ckfree (listArgv);
  378.     return TCL_ERROR;
  379.  
  380. } /* SippPrism */
  381.  
  382. /*=============================================================================
  383.  * SippBlock --
  384.  *   Implements the command:
  385.  *      SippBlock {xsize ysize zsize} shaderhandle [texture]
  386.  * Note:
  387.  *   This procedure has standard Tcl command calling sematics.  ClientData
  388.  * contains a pointer to the Tcl SIPP global structure.
  389.  *-----------------------------------------------------------------------------
  390.  */
  391. int
  392. SippBlock (clientData, interp, argc, argv)
  393.     char       *clientData;
  394.     Tcl_Interp *interp;
  395.     int         argc;
  396.     char      **argv;
  397. {
  398.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  399.     Shader        *shaderPtr;
  400.     void          *surfDescPtr;
  401.     Vector         sizes;
  402.     int            texture = NATURAL;
  403.  
  404.     if ((argc < 3) || (argc > 4)) {
  405.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  406.                           " {xsize ysize zsize} shaderhandle [texture]",
  407.                           (char *) NULL);
  408.         return TCL_ERROR;
  409.     }                     
  410.  
  411.     if (!TSippConvertVertex (tSippGlobPtr, argv [1], &sizes))
  412.         return TCL_ERROR;
  413.     if ((sizes.x <= 0.0) || (sizes.y <= 0.0) || (sizes.z <= 0.0)) {
  414.         Tcl_AppendResult (tSippGlobPtr->interp, " sizes X Y and Z must be ",
  415.                           " > 0.0", (char *) NULL);
  416.         return TCL_ERROR;
  417.     }
  418.  
  419.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [2],
  420.                                         &surfDescPtr);
  421.     if (shaderPtr == NULL)
  422.         return TCL_ERROR;
  423.  
  424.     if (argc == 4) {
  425.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [3], &texture, NULL))
  426.             return TCL_ERROR;
  427.     }
  428.  
  429.     TSippBindObjectToHandle (tSippGlobPtr, 
  430.                              sipp_block (sizes.x, sizes.y, sizes.z, 
  431.                                          surfDescPtr, shaderPtr, texture)); 
  432.     return TCL_OK;
  433.  
  434. } /* SippBlock */
  435.  
  436. /*=============================================================================
  437.  * SippCube --
  438.  *   Implements the command:
  439.  *      SippCube size shaderhandle [texture]
  440.  * Note:
  441.  *   This procedure has standard Tcl command calling sematics.  ClientData
  442.  * contains a pointer to the Tcl SIPP global structure.
  443.  *-----------------------------------------------------------------------------
  444.  */
  445. int
  446. SippCube (clientData, interp, argc, argv)
  447.     char       *clientData;
  448.     Tcl_Interp *interp;
  449.     int         argc;
  450.     char      **argv;
  451. {
  452.     tSippGlob_pt   tSippGlobPtr = (tSippGlob_pt) clientData;
  453.     Shader        *shaderPtr;
  454.     void          *surfDescPtr;
  455.     double         size;
  456.     int            texture = NATURAL;
  457.  
  458.     if ((argc < 3) || (argc > 4)) {
  459.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  460.                           " size shaderhandle [texture]", (char *) NULL);
  461.         return TCL_ERROR;
  462.     }                     
  463.     if (!TSippConvertUnsignedDbl (tSippGlobPtr, argv [1], &size))
  464.          return TCL_ERROR;
  465.  
  466.     shaderPtr = TSippShaderHandleToPtr (tSippGlobPtr, argv [2],
  467.                                         &surfDescPtr);
  468.     if (shaderPtr == NULL)
  469.         return TCL_ERROR;
  470.  
  471.     if (argc == 4) {
  472.         if (!TSippParseTextureMapping (tSippGlobPtr, argv [3], &texture, NULL))
  473.             return TCL_ERROR;
  474.     }
  475.     TSippBindObjectToHandle (tSippGlobPtr, 
  476.                              sipp_cube (size, surfDescPtr, shaderPtr,
  477.                                         texture)); 
  478.     return TCL_OK;
  479.  
  480. } /* SippCube */
  481.  
  482. /*=============================================================================
  483.  * TSippPrimInit --
  484.  *   Initialized the primitve object creation commands.
  485.  *
  486.  * Parameters:
  487.  *   o tSippGlobPtr (I) - Pointer to the top level global data structure.
  488.  *-----------------------------------------------------------------------------
  489.  */
  490. void
  491. TSippPrimInit (tSippGlobPtr)
  492.     tSippGlob_pt  tSippGlobPtr;
  493. {
  494.     static tSippTclCmdTbl_t cmdTable [] = {
  495.         {"SippTorus",      SippTorus},
  496.         {"SippCone",       SippCone},
  497.         {"SippCylinder",   SippCylinder},
  498.         {"SippEllipsoid",  SippEllipsoid},
  499.         {"SippSphere",     SippSphere},
  500.         {"SippPrism",      SippPrism},
  501.         {"SippBlock",      SippBlock},
  502.         {"SippCube",       SippCube},
  503.         {NULL,             NULL}
  504.     };
  505.  
  506.     TSippInitCmds (tSippGlobPtr, cmdTable);
  507.  
  508. } /* TSippPrimInit */
  509.  
  510.  
  511.